package com.txq.bilibili.app;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import com.txq.bilibili.app.fragments.*;


public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    private LinearLayout linearNew;
    private LinearLayout linearSearch;
    private LinearLayout linearTry;
    private LinearLayout linearSocial;
    private LinearLayout linearPersonal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearNew = (LinearLayout) findViewById(R.id.common_linear_new);
        linearSearch = (LinearLayout) findViewById(R.id.common_linear_search);
        linearTry = (LinearLayout) findViewById(R.id.common_linear_try);
        linearSocial = (LinearLayout) findViewById(R.id.common_linear_social);
        linearPersonal = (LinearLayout) findViewById(R.id.common_linear_personal);

        linearNew.setOnClickListener(this);
        linearSearch.setOnClickListener(this);
        linearTry.setOnClickListener(this);
        linearSocial.setOnClickListener(this);
        linearPersonal.setOnClickListener(this);

        NewStyleFragment newStyleFragment = new NewStyleFragment();

        replaceFragment(newStyleFragment);
        linearNew.setBackgroundResource(R.drawable.common_bottom_style);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.common_linear_new:

                NewStyleFragment newStyleFragment = new NewStyleFragment();

                replaceFragment(newStyleFragment);
                linearNew.setBackgroundResource(R.drawable.common_bottom_style);
                linearPersonal.setBackgroundResource(R.drawable.myGray);
                linearTry.setBackgroundResource(R.drawable.myGray);
                linearSocial.setBackgroundResource(R.drawable.myGray);
                linearSearch.setBackgroundResource(R.drawable.myGray);

                break;
            case R.id.common_linear_search:
                SearchFragment searchFragment = new SearchFragment();
                replaceFragment(searchFragment);
                linearNew.setBackgroundResource(R.drawable.myGray);
                linearPersonal.setBackgroundResource(R.drawable.myGray);
                linearTry.setBackgroundResource(R.drawable.myGray);
                linearSocial.setBackgroundResource(R.drawable.myGray);
                linearSearch.setBackgroundResource(R.drawable.common_bottom_style);
                break;
            case R.id.common_linear_try:
                TryFragment tryFragment = new TryFragment();
                replaceFragment(tryFragment);
                linearNew.setBackgroundResource(R.drawable.myGray);
                linearPersonal.setBackgroundResource(R.drawable.myGray);
                linearTry.setBackgroundResource(R.drawable.common_bottom_style);
                linearSocial.setBackgroundResource(R.drawable.myGray);
                linearSearch.setBackgroundResource(R.drawable.myGray);
                break;
            case R.id.common_linear_social:
                SocialFragment socialFragment = new SocialFragment();
                replaceFragment(socialFragment);
                linearNew.setBackgroundResource(R.drawable.myGray);
                linearPersonal.setBackgroundResource(R.drawable.myGray);
                linearTry.setBackgroundResource(R.drawable.myGray);
                linearSocial.setBackgroundResource(R.drawable.common_bottom_style);
                linearSearch.setBackgroundResource(R.drawable.myGray);
                break;
            case R.id.common_linear_personal:
                PersonalFragment personalFragment = new PersonalFragment();
                replaceFragment(personalFragment);
                linearNew.setBackgroundResource(R.drawable.myGray);
                linearPersonal.setBackgroundResource(R.drawable.common_bottom_style);
                linearTry.setBackgroundResource(R.drawable.myGray);
                linearSocial.setBackgroundResource(R.drawable.myGray);
                linearSearch.setBackgroundResource(R.drawable.myGray);
                break;
        }
    }

    private void replaceFragment(Fragment newStyleFragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        transaction.replace(R.id.main_frame, newStyleFragment);

        transaction.commit();
    }


}



